<?php


namespace app\api\controller;
use \app\common\business\Cart as CartBiz;

use app\common\lib\Show;

class Cart extends AuthBase {

    /**
     * 购物车列表
     * @return \think\response\Json
     */
    public function lists() {
        $res = (new CartBiz())->lists($this->userId);
        if ($res === FALSE) {
            return Show::error();
        }
        return Show::success($res);
    }

    /**
     * 添加到购物车
     * @return \think\response\Json
     */
    public function add() {
        if (!$this->request->isPost()) {
            return Show::error();
        }
        //商品的sku_id
        $id = $this->request->param('id', 0, 'intval');
        $num = $this->request->param('num', 0, 'intval');
        if ($id == 0 || $num == 0) {
            return Show::error('参数错误');
        }
        //添加到redis
        $res = (new CartBiz())->insertToRedis($this->userId, $id, $num);
        if ($res === FALSE) {
            return Show::error();
        }
        return Show::success();
    }

    //删除
    public function delete() {
        if (!$this->request->isPost()) {
            return Show::error();
        }

        $id = input("param.id", 0, "intval");
        if (!$id) {
            return Show::error("参数不合法");
        }
        $res = (new CartBiz())->deleteRedis($this->userId, $id);
        if ($res === FALSE) {
            return Show::error();
        }
        return Show::success($res);
    }

    /**
     * 清空
     * @return \think\response\Json
     */
    public function destoryAll() {
        if (!$this->request->isPost()) {
            return Show::error();
        }
        $ids = (new CartBiz())->getKeys($this->userId);
        $res = (new CartBiz())->deleteRedis($this->userId, $ids);
        if ($res === FALSE) {
            return Show::error();
        }
        return Show::success($res);
    }

    /**
     * 修改
     * @return \think\response\Json
     */
    public function update() {
        if (!$this->request->isPost()) {
            return Show::error();
        }

        $id = input("param.id", 0, "intval");
        $num = input("param.num", 0, "intval");
        $selected = input("param.selected", 0, "intval");
        if (!$id || !$num) {
            return Show::error("参数不合法");
        }
        $data = [
            'num' => $num,
            'selected'=> $selected
        ];
        try {
            $res = (new CartBiz())->updateRedis($this->userId, $id, $data);
        } catch (\think\Exception $e) {
            return Show::error($e->getMessage());
        }
        if ($res === FALSE) {
            return Show::error();
        }
        return Show::success($res);
    }

    /**
     * 获取购物车商品数量
     * @return \think\response\Json
     */
    public function getCount() {
        $res = (new CartBiz())->getCount($this->userId);
        return Show::success(['count'=> $res]);
    }

    public function selectAll() {
        $res = (new CartBiz())->changeAll($this->userId, 1);
        return Show::success($res);
    }
    public function unSelectAll() {
        $res = (new CartBiz())->changeAll($this->userId, 0);
        return Show::success($res);
    }

    /**
     * 获取购物车已选商品
     */
    public function getSlectedList() {
        $res = (new CartBiz())->getSlectedList($this->userId);
        return Show::success($res);
    }
}